#include "LogLayout.h"
#include <thread>

namespace log
{

std::string LogLayout::format(const LogEvent::ptr ptrEvent)
{
    std::ostringstream os;
    for (auto item : m_vecItems)
    {
        item->format(os, ptrEvent);
    }
    os << "\n";
    return os.str();
}

void YearLayoutItem::format(std::ostream& os, const LogEvent::ptr ptrEvent)
{
    os << ptrEvent->getTime().substr(0, 4);
}

void MouthLayoutItem::format(std::ostream& os, const LogEvent::ptr ptrEvent)
{
    os << ptrEvent->getTime().substr(4, 2);
}

void DayLayoutItem::format(std::ostream& os, const LogEvent::ptr ptrEvent)
{
    os << ptrEvent->getTime().substr(6, 2);
}

void HourLayoutItem::format(std::ostream& os, const LogEvent::ptr ptrEvent)
{
    os << ptrEvent->getTime().substr(8, 2);
}

void MinLayoutItem::format(std::ostream& os, const LogEvent::ptr ptrEvent)
{
    os << ptrEvent->getTime().substr(10, 2);
}

void SecondLayoutItem::format(std::ostream& os, const LogEvent::ptr ptrEvent)
{
    os << ptrEvent->getTime().substr(12, 2);
}

void MSecLayoutItem::format(std::ostream& os, const LogEvent::ptr ptrEvent)
{
}

void LevelLayoutItem::format(std::ostream& os, const LogEvent::ptr ptrEvent)
{
    os << LogLevel::toString(ptrEvent->getLevel());
}

void ThreadIdLayoutItem::format(std::ostream& os, const LogEvent::ptr ptrEvent)
{
    os << ptrEvent->getTid();
}

void FileLayoutItem::format(std::ostream& os, const LogEvent::ptr ptrEvent)
{
    os << ptrEvent->getFile();
}

void LineLayoutItem::format(std::ostream& os, const LogEvent::ptr ptrEvent)
{
    os << ptrEvent->getLine();
}

void FuncLayoutItem::format(std::ostream& os, const LogEvent::ptr ptrEvent)
{
    os << ptrEvent->getFunc();
}

void MsgLayoutItem::format(std::ostream& os, const LogEvent::ptr ptrEvent)
{
    os << ptrEvent->getMsg();
}

void StringLayoutItem::format(std::ostream& os, const LogEvent::ptr ptrEvent)
{
    os << m_str;
}

} // namespace log
